﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Demo.UserService.Entities;

namespace Demo.UserService.Data;

public class MicroserviceDbContext : AbpDbContext<MicroserviceDbContext>
{
    public MicroserviceDbContext(DbContextOptions<MicroserviceDbContext> options)
        : base(options)
    {
    }

    /// <summary>
    /// 用户表
    /// </summary>
    public   DbSet<ShopUserInfo> UserInfoDb { get; set; }
    /// <summary>
    /// 用户地址表
    /// </summary>
    public  DbSet<ShopUserAddress> UserAddressDb { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        // 设置所有decimal类型的默认精度和长度
        var decimalProperties = builder.Model.GetEntityTypes().SelectMany(t => t.GetProperties()).Where(p => p.ClrType == typeof(decimal));
        foreach (var property in decimalProperties)
        {
            property.SetColumnType("decimal(10, 2)"); // 设置精度为10，小数位数为2
        }

        // 设置所有varchar类型默认长度
        var varcharProperties = builder.Model.GetEntityTypes().SelectMany(it => it.GetProperties()).Where(p => p.ClrType == typeof(string));
        foreach (var property in varcharProperties)
        {

        }


        base.OnModelCreating(builder);

        /* Include modules to your migration db context */


        /* Configure your own entities here */
    }

    /// <summary>
    /// 全局配置字段上的一些属性
    /// </summary>
    /// <param name="configurationBuilder"></param>
    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder
            .Properties<string>()
            .AreUnicode(false)
            .HaveMaxLength(1024);

        //1、decimal的精度控制
        //configurationBuilder.Properties<decimal>()

    }

}
